﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Hosting
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using ConnectBasic.Shell.Extensibility;
    using ConnectBasic.Shell.Implementation;

    /// <summary>
    /// Provides the default implementation of the
    /// <see cref="IShellHostProjectCollection" /> interface.
    /// </summary>
    internal class DefaultShellHostProjectCollection : IShellHostProjectCollection
    {
        private readonly IShellHost mHost;
        private readonly Func<IProjectStore, IProject> mFactory;
        private readonly List<IProject> mProjects = new List<IProject>();

        #region .ctor

        internal DefaultShellHostProjectCollection(IShellHost host) : this(host, null)
        {

        }

        internal DefaultShellHostProjectCollection(IShellHost host, Func<IProjectStore, IProject> projectFactory)
        {
            mHost = host;

            // Make sure we've got a project factory
            if (projectFactory == null)
            {
                mFactory = (store) => new DefaultProject(host.Shell, store);
            }
            else
            {
                mFactory = projectFactory;
            }
        }

        #endregion

        #region IShellHostProjectCollection Members

        private EventHandler<ProjectEventArgs> mProjectLoadedEvent;

        event EventHandler<ProjectEventArgs> IShellHostProjectCollection.ProjectLoaded
        {
            add { mProjectLoadedEvent += value; }
            remove { mProjectLoadedEvent -= value; }
        }

        IProject IShellHostProjectCollection.LoadProject(IProjectStore projectStore)
        {
            if (projectStore == null) throw new ArgumentNullException("projectStore");

            // Create the project
            var project = mFactory(projectStore);

            if (project == null)
            {
                return null;
            }

            // Register it and return it to the caller
            mProjects.Add(project);
            return project;
        }

        int IShellHostProjectCollection.Count
        {
            get { return mProjects.Count; }
        }

        IProject IShellHostProjectCollection.this[int index]
        {
            get { return mProjects[index]; }
        }

        #endregion

        #region IEnumerable<IProject> Members

        IEnumerator<IProject> IEnumerable<IProject>.GetEnumerator()
        {
            return mProjects.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return mProjects.GetEnumerator();
        }

        #endregion

    }
}
